﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Protocol;
using Pixysoft.Weblications.Sango.Protocol.player;

namespace Pixysoft.Weblications.Sango.Command.equipment
{
    partial class SangoEquipmentHandler
    {
        public eqm_sell eqm_sell(string primarycode)
        {
            string userCode = SangoManager.GetUsercode();

            eqm_sell response = ReflectionManager.CreatePojo<eqm_sell>();
            response.Succeed = false;

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);
            Entities.Usr_Property property = UsrPropertyDao.getProperty(userCode);
            Entities.Usr_Equipment eqm = EquipmentDao.tryGetUserEqm(primarycode);

            if (eqm == null)
            {
                response.Message = "装备不存在";
                return response;
            }

            Entities.Eqm_Equipment syseqm = EqmEquipmentHelper.searchBySerialcode(eqm.Equipmentserialcode);

            if (eqm.Equipmentstatus == EquipmentStatusCollection.WEAR)
            {
                response.Message = "正在装配中，无法出售。";
                return response;
            }

            long cashvalue = EquipmentFormular.getSellprice(syseqm, eqm);
            if (!UsrProfileDao.tryIncreaseCashvalue(profile, cashvalue))
            {
                response.Message = "银币到达上限，无法出售";
                return response;
            }
            eqm.Equipmentstatus = EquipmentStatusCollection.CLOSE;

            UsrPropertyDao.leaveRepository(property);

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;
            command.Begin(delegate()
            {
                command.Update(profile);
                command.Update(eqm);
                command.Update(property);
                command.Commit();
            });

            response.property = UsrProfileFacade.ToProperty(property);
            response.profile = UsrProfileFacade.ToProfile(profile);
            response.Succeed = true;
            return response;
        }


    }
    public interface eqm_sell : ISangoResponse
    {
        player_profile profile { get;set;}
        player_property property { get;set;}
    }
}
